package com.joke.mydiary.DaoImpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.joke.mydiary.pojo.Diary;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 日记Dao
 * Created by Administrator on 2015/11/30.
 */
public class DiaryDao implements Dao<Integer, Diary> {
    private DatabaseHelper helper;
    private SimpleDateFormat sdf;
    private static final String TABLENAME = "diaries";

    public DiaryDao(Context context) {
        this.helper = new DatabaseHelper(context);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 插入日记
     *
     * @param diary
     * @return
     */
    @Override
    public boolean insert(Diary diary) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("date", sdf.format(diary.getDate()));
        values.put("content", diary.getContent());
        values.put("image_path", diary.getImagePath());
        long rowId = db.insert(TABLENAME, null, values);
        db.close();
        if (rowId > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Diary diary) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("date", sdf.format(diary.getDate()));
        values.put("content", diary.getContent());
        values.put("image_path", diary.getImagePath());
        String where = "_id=?";
        String[] args = {String.valueOf(diary.get_id())};
        int rowId = db.update(TABLENAME, values, where, args);
        db.close();
        if (rowId > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String where = "_id=?";
        String[] args = {String.valueOf(id)};
        int rowId = db.delete(TABLENAME, where, args);
        db.close();
        if (rowId > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过时间删除，上下文菜单中使用
     * @param time
     * @return
     */
    public boolean deleteByTime(String time) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String where = "date=?";
        String[] args = {time};
        int rowId = db.delete(TABLENAME, where, args);
        if (rowId > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Diary findById(Integer id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "SELECT date,content,image_path FROM " + TABLENAME + "where _id=?";
        String[] args = {String.valueOf(id)};
        Cursor cursor = db.rawQuery(sql, args);
        Diary diary = null;
        if (cursor.moveToNext()) {
            diary = new Diary();
            diary.set_id(id);
            try {
                diary.setDate(sdf.parse(cursor.getString(cursor.getColumnIndex("date"))));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            diary.setContent(cursor.getString(cursor.getColumnIndexOrThrow("content")));
            diary.setImagePath(cursor.getString(cursor.getColumnIndex("image_path")));
            return diary;
        }
        return null;
    }

    @Override
    public ArrayList<Diary> findAll() {
        SQLiteDatabase db = helper.getWritableDatabase();
        ArrayList<Diary> list = new ArrayList<>();
        Cursor cursor = db.query(true, TABLENAME, null, null, null, null, null, "_id DESC", null);
        while (cursor.moveToNext()) {
            Diary d = new Diary();
            d.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            d.setDate(new Timestamp(cursor.getLong(cursor.getColumnIndexOrThrow("date"))));
            d.setContent(cursor.getString(cursor.getColumnIndexOrThrow("content")));
            d.setImagePath(cursor.getString(cursor.getColumnIndex("image_path")));
            list.add(d);
        }
        return list;
    }

    public Cursor findAllCursor() {
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(true, TABLENAME, null, null, null, null, null, "_id DESC", null);
        return cursor;
    }

    /**
     * 通过模糊查询返回游标
     *
     * @param key
     * @return
     */
    public Cursor findByKeyCursor(String key) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "SELECT * FROM diaries WHERE date LIKE ? ORDER BY _id DESC";
        String[] selectionArgs = {"%" + key + "%"};
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        return cursor;
    }


    public Diary findByTime(String time) {
        SQLiteDatabase db = helper.getReadableDatabase();
        String selection = "date=?";
        String[] args = {time};
        Cursor cursor = db.query(true, TABLENAME, null, selection, args, null, null, null, null);
        Diary diary = null;
        if (cursor.moveToNext()) {
            diary = new Diary();
            diary.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            try {
                diary.setDate(sdf.parse(cursor.getString(cursor.getColumnIndex("date"))));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            diary.setContent(cursor.getString(cursor.getColumnIndexOrThrow("content")));
            diary.setImagePath(cursor.getString(cursor.getColumnIndex("image_path")));
            return diary;
        }
        return null;
    }

    /**
     * 未实现
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public ArrayList<Diary> findPageAll(int currentPage, int pageSize) {
        SQLiteDatabase db = helper.getWritableDatabase();
        return null;
    }
}
